const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contactUniqueId:null,
    deliveryId:null,
    vitaeItems: [],
    vitaeItemsIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      deliveryId:options.deliveryId,
      contactUniqueId:options.uniqueId
    })
    wx.request({
      url: app.globalData.url + '/msg/getMsgTemplate',
      data: {
        uniqueId: app.globalData.uniqueId
      },
      success: res => {
        var vitaeTitles = res.data
        var vitaeItems = []
        if (vitaeTitles.length != 0) {
          vitaeTitles.forEach(function(item, index) {
            vitaeItems.push({
              name: item.text,
              value: index,
              checked: false
            })
          })
        }
        if (vitaeItems.length != 0) {
          vitaeItems[0].checked = true;
          that.setData({
            vitaeItems: vitaeItems,
          })
        }
      }
    })
  },
  vitaeItemsChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var vitaeItems = this.data.vitaeItems;
    for (var i = 0, len = vitaeItems.length; i < len; ++i) {
      vitaeItems[i].checked = vitaeItems[i].value == e.detail.value;
    }
    this.setData({
      vitaeItems: vitaeItems,
      vitaeItemsIndex: e.detail.value,
    });
  },

  formSubmit: function() {
    var that = this
    wx.showModal({
      title: '提示',
      content: "确认选择该模板",
      success: function(res) {
        if (res.confirm) {
          that.send();
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })

  },
  send: function() {
    var that = this
    wx.request({
      url: app.globalData.url + '/msg/sendMsg',
      data: {
        uniqueId: app.globalData.uniqueId,
        contactUniqueId: that.data.contactUniqueId,
        text: that.data.vitaeItems[that.data.vitaeItemsIndex].name
      },
      success: res => {
        if (res.data.code == 0) {
          wx.showModal({
            title: '失败',
            content: '请联系管理员',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '通知成功，返回页面',
            success: function(res) {
              if (res.confirm) {
                wx.request({
                  url: app.globalData.url +'/vitae/changeDeliveryCode',
                  data:{
                    id:that.data.deliveryId,
                    code:2
                  }
                })
                wx.navigateBack({
                  delta: 2
                })
                console.log('用户点击确定')
              } else {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})